import { _decorator, Color, Component, instantiate, Node, Prefab, Sprite, UITransform } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { UserInfo } from '../common/UserInfo';
import { LocationType, Soldier } from '../entity/Soldier';

import { EventManager } from '../event/EventManager';

import VillageApi from '../api/VillageApi';
import { EventType } from '../settings/const';
import { TrainEvent } from '../entity/TrainEvent';

import { SoldierCover } from '../train/SoldierCover';
import { SoldierTrainItem } from '../train/SoldierTrainItem';
import ResManager from '../common/ResManager';
import { UIHeader } from '../common/UIHeader';
import { TabLayout } from '../common/TabLayout';
const { ccclass, property } = _decorator;

@ccclass('PalaceIndex')
export class PalaceIndex extends Component {

    public static instance:PalaceIndex
    @property(UIHeader)
    uiHeader:UIHeader
    @property(TabLayout)
    tabLayout:TabLayout

    // @property(Node)
    // close: Node

    // @property(Node)
    // train: Node
    // @property(Node)
    // civil: Node
    // @property(Node)
    // loyalty: Node
    // @property(Node)
    // expand: Node

    @property(Node)
    content: Node
    @property(Prefab)
    palaceTrain:Prefab
    @property(Prefab)
    palaceCivil:Prefab
    @property(Prefab)
    palaceLoyalty:Prefab
    @property(Prefab)
    palaceExpand:Prefab


    private soldierList: Soldier[] = []

    
    protected onLoad(): void {
        PalaceIndex.instance = this
    }

    start() {
        this.uiHeader.setData('宫殿')
        this.tabLayout.setData(['文明度','忠诚度','扩张'])
        // EventManager.instance.on('训练',this.onTrain, this)
        EventManager.instance.on('文明度',this.onCivil, this)
        EventManager.instance.on('忠诚度',this.onLoyalty, this)
        EventManager.instance.on('扩张',this.onExpand, this)
        EventManager.instance.on('宫殿close', this.onClose, this)

      
       
        this.onCivil()

    }

    
    async onTrain() {
      
      

        this.content.destroyAllChildren()
       
        let node = instantiate(this.palaceTrain)
        this.content.addChild(node)

    }

    onCivil() {
     
       
        this.content.destroyAllChildren()
        let node = instantiate(this.palaceCivil)
        this.content.addChild(node)
    }

    onLoyalty() {
      
        
        this.content.destroyAllChildren()
        let node = instantiate(this.palaceLoyalty)
        this.content.addChild(node)
    }

    onExpand() {
       
       
        this.content.destroyAllChildren()
        let node = instantiate(this.palaceExpand)
        this.content.addChild(node)
    }

  

    renderCoverList(list: Soldier[]) {
        // this.soldierList = list
        // this.coverContent.destroyAllChildren()
        // for (let item of list) {
        //     let node = instantiate(this.SoldierCover)
        //     this.coverContent.addChild(node)
        //     node.getComponent(SoldierCover).setData(item)
        // }

        // UIManager.instance.closeUI(PrefabEnum.ProPanel)
    }

    /**
     * 处理TAB按钮，如果是主村的话显示三个，如果是副村的话显示五个
     */
    handleTab() {
        // if (UserInfo.instance.isMainVillage) {
        //     this.bigBarracks.active = false
        //     this.bigStable.active = false

        //     this.barracks.getComponent(UITransform).width = 250
        //     this.stable.getComponent(UITransform).width = 250
        //     this.workshop.getComponent(UITransform).width = 250
        // }
    }

   

    onClose() {
        UIManager.instance.closeUI(PrefabEnum.PalaceIndex)
        UIManager.instance.closeUI(PrefabEnum.ProPanel)
        UIManager.instance.closeUI(PrefabEnum.TrainNumber)
    }

    

}


